package com.neusoft.neuiotms.dcom.service.change.impl;

import javax.persistence.EntityNotFoundException;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.activiti.enums.WorkOrderStatus;
import com.neusoft.bizcore.activiti.service.ProcessInstanceService;
import com.neusoft.bizcore.activiti.service.WorkOrderService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.file.service.FileService;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcom.converter.change.ChangeImplementationConverter;
import com.neusoft.neuiotms.dcom.dto.change.ChangeImplementationDTO;
import com.neusoft.neuiotms.dcom.model.change.ChangeImplementation;
import com.neusoft.neuiotms.dcom.repository.change.ChangeImplementationAttachmentRepository;
import com.neusoft.neuiotms.dcom.repository.change.ChangeImplementationRepository;
import com.neusoft.neuiotms.dcom.service.change.ChangeImplementationService;

@Service
public class ChangeImplementationServiceImpl implements ChangeImplementationService {

    @Autowired
    private ChangeImplementationRepository changeImplementationRepository;

    @Autowired
    private ChangeImplementationConverter changeImplementationConverter;

    @Autowired
    private ChangeImplementationAttachmentRepository changeImplementationAttachmentRepository;

    @Autowired
    private FileService fileService;

    @Autowired
    private WorkOrderService workOrderService;

    @Autowired
    private ProcessInstanceService processInstanceService;

    @Override
    @Transactional
    public ChangeImplementationDTO create(final ChangeImplementationDTO dto, final UserBean user) {
        if (WorkOrderStatus.temporary.name().equals(dto.getStatus())) {
            final Object title = this.processInstanceService.getVariables(dto.getWorkOrderNum(), "title", "String");
            final String workOrderNum =
                    this.workOrderService.temporaryStorage(title.toString(), "change", dto.getWorkOrderNum(), user);
            dto.setWorkOrderNum(workOrderNum);
        } else {
            // 完成节点
            this.processInstanceService.complete(dto.getWorkOrderNum(), dto.getTaskId(), user);
        }
        // 查询当前workNum和TaskId下是否存在记录
        final ChangeImplementation oldModel = this.changeImplementationRepository.findByWorkOrderNumAndTaskId(
                dto.getWorkOrderNum(), dto.getTaskId());
        if (oldModel == null) { // 如果不存在新增
            dto.setId(null);
        }

        final ChangeImplementation model = this.changeImplementationConverter.toModel(dto);
        this.changeImplementationRepository.save(model);

        return this.changeImplementationConverter.toDTO(model);

    }

    @Override
    public ChangeImplementationDTO detail(final String workOrderNum, final String taskId, final UserBean user) {
        ChangeImplementation model = null;
        try {
            if (StringUtils.isNotBlank(workOrderNum) && !"NULL".equals(taskId)) {
                model = this.changeImplementationRepository.findByWorkOrderNumAndTaskId(workOrderNum, taskId);
                if (model == null) {
                    // 获取前一节点的TaskID
                    final String lastTaskId = this.processInstanceService.getLastTaskId(taskId, user);
                    if (lastTaskId == null) {
                        return new ChangeImplementationDTO();
                    } else {
                        model = this.changeImplementationRepository.findByWorkOrderNumAndTaskId(workOrderNum,
                                lastTaskId);
                    }
                }
            } else if ("NULL".equals(taskId)) {
                model = this.changeImplementationRepository.findByWorkOrderNum(workOrderNum);
            }
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                    new Object[] {workOrderNum },
                    new Object[] {taskId });
        }
        return this.changeImplementationConverter.toDTO(model);
    }

    @Override
    public ChangeImplementationDTO findByWorkOrderNumAndTaskId(final String workOrderNum, final String taskId) {
        final ChangeImplementation model =
                this.changeImplementationRepository.findByWorkOrderNumAndTaskId(workOrderNum, taskId);
        return this.changeImplementationConverter.toDTO(model);
    }

    @Override
    @Transactional
    public void deleteMongoFile(final String fsId) {
        this.fileService.deleteByFsId(fsId);
        this.changeImplementationAttachmentRepository.deleteByFsId(fsId);
    }

    @Override
    public void deleteByWorkOrderNum(final String workOrderNum) {
        this.changeImplementationRepository.deleteByWorkOrderNum(workOrderNum);
    }

    @Override
    public ChangeImplementationDTO findByWorkNum(final String workOrderNum) {
        final ChangeImplementation model = this.changeImplementationRepository.findByWorkNum(workOrderNum);
        if (model != null) {
            return this.changeImplementationConverter.toDTO(model);
        } else {
            return null;
        }
    }

}
